package com.roadjava.rating.mapper;



import com.roadjava.rating.bean.entity.RatingDO;
import com.roadjava.rating.bean.req.rating.RatingReq;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RatingMapper {

    // 添加评分
    @Insert("INSERT INTO rating (resourceid, id, score) VALUES (#{resourceid}, #{id}, #{score})")
    void addRating(RatingReq rating);

    // 更新评分
    @Update("UPDATE rating SET score = #{score} WHERE ratingid = #{ratingId}")
    void updateRating(@Param("ratingId") Integer ratingId, @Param("score") Integer score);

    // 删除评分
    @Delete("DELETE FROM rating WHERE ratingid = #{ratingId}")
    void deleteRating(@Param("ratingId") Integer ratingId);

    // 根据资源ID和用户ID获取评分
    @Select("SELECT * FROM rating WHERE resourceid = #{resourceId} AND id = #{userId}")
    RatingDO getRatingByResourceIdAndUserId(@Param("resourceId") Integer resourceId, @Param("userId") Long userId);

    // 获取资源的平均评分
    @Select("SELECT AVG(Score) FROM rating WHERE resourceid = #{resourceId}")
    Double getAverageRating(@Param("resourceId") Integer resourceId);

    // 获取所有评分
    @Select("SELECT * FROM rating")
    List<RatingDO> getAllRatings();

    // 根据资源ID获取评分列表
    @Select("SELECT * FROM rating WHERE resourceid = #{resourceId}")
    List<RatingDO> getRatingsByResourceId(@Param("resourceId") Integer resourceId);

    // 根据用户ID获取评分列表
    @Select("SELECT * FROM rating WHERE id = #{userId}")
    List<RatingDO> getRatingsByUserId(@Param("userId") Long userId);
}
